{- This file is part of funbot.
 -
 - Written in 2015, 2016 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

-- | Tell command
--
-- Tell something to some other user
module FunBot.Commands.Memos
    ( cmdPTell
    , cmdCTell
    , cmdPAsk
    , cmdCAsk
    )
where

import Control.Monad (unless, when)
import Data.List (find, intercalate)
import Data.Monoid ((<>))
import Data.Settings.Types (showOption)
import Data.Text (Text)
import FunBot.History (quote, reportHistory')
import FunBot.Memos (submitMemo)
import FunBot.Settings
import FunBot.Settings.Sections.Channels (addChannel)
import FunBot.Settings.Sections.Feeds (addFeed, deleteFeed)
import FunBot.Settings.Sections.Repos
import FunBot.Settings.Sections.Shortcuts (addShortcut, deleteShortcut)
import FunBot.Types
import FunBot.UserOptions
import FunBot.Util
import Network.IRC.Fun.Bot.Behavior
import Network.IRC.Fun.Bot.Chat
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Bot.Types
import Text.Read (readMaybe)
import Network.IRC.Fun.Types.Base

import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.Read as TR

-- Given whether to always send privately and whether the memo is a question,
-- return a command response
respondTell
    :: Bool
    -> Bool
    -> Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondTell priv ask mchan sender (recip : ws@(_:_)) send =
    let unsnoc t =
            if T.null t
                then Nothing
                else Just (T.init t, T.last t)
        recip' =
            case unsnoc recip of
                Just (r, ':') -> r
                Just (r, ',') -> r
                _             -> recip
        msg = MsgContent $ T.unwords ws
    in  if looksLikeNick recip'
            then submitMemo sender mchan (Nickname recip') priv ask msg
            else send $ notnick recip'
respondTell _priv _ask mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) Nothing

cmdPTell = Command
    { cmdNames    = cmds ["tell", "ptell"]
    , cmdRespond  = respondTell True False
    , cmdHelp     = helps
        [ ( "tell <nick>[:|,] <text>"
          , "leave a memo for a user to see later. Memos can be sent to the \
            \recipient privately, or publicly in the channel in which they \
            \were submitted. With this command, the memo will be sent \
            \privately. If that isn't your intention, see the ctell command."
          )
        ]
    , cmdExamples =
        [ "tell johndoe Hello! How are you?"
        , "tell johndoe, Hello! How are you?"
        , "tell johndoe: Hello! How are you?"
        ]
    }

cmdCTell = Command
    { cmdNames    = cmds ["ctell"]
    , cmdRespond  = respondTell False False
    , cmdHelp     = helps
        [ ( "tell <nick>[:|,] <text>"
          , "leave a memo for a user to see later. Memos can be sent to the \
            \recipient privately, or publicly in the channel in which they \
            \were submitted. With this command, the memo will be sent in the \
            \same way it was submitted: If you submit it in a channel, it \
            \will be sent to the recipient in the same channel. If you submit \
            \using a private message to me, I will also send it privately to \
            \the recipient.\n\
            \If that isn't your intention, see the tell command."
          )
        ]
    , cmdExamples =
        [ "ctell johndoe Hello! How are you?"
        , "ctell johndoe, Hello! How are you?"
        , "ctell johndoe: Hello! How are you?"
        ]
    }

cmdPAsk = Command
    { cmdNames    = cmds ["ask", "pask"]
    , cmdRespond  = respondTell True True
    , cmdHelp     = helps
        [ ( "ask <nick>[:|,] <text>"
          , "leave a memo for a user to see later. \
            \Similar to the tell command."
          )
        ]
    , cmdExamples =
        [ "ask johndoe whether to quit now"
        ]
    }

cmdCAsk = Command
    { cmdNames    = cmds ["cask"]
    , cmdRespond  = respondTell False True
    , cmdHelp     = helps
        [ ( "ask <nick>[:|,] <text>"
          , "leave a memo for a user to see later. \
            \Similar to the ctell command."
          )
        ]
    , cmdExamples =
        [ "cask johndoe whether to quit now"
        ]
    }
